/* GUI CONVERTED from sosotrl2.i (converter v1.76) Wed Jul 17 23:11:05 2002 */
/* sosotrl2.i - SALES ORDER TRAILER - TAX MANAGEMENT                          */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.28 $                                                          */
/*V8:ConvertMode=ReportAndMaintenance                                         */
/* REVISION: 7.4            CREATED: 10/02/95   BY: jym *G0XY*                */
/* REVISION: 8.5      LAST MODIFIED: 07/22/93   BY: bcm *H032*                */
/* REVISION: 8.5      LAST MODIFIED: 07/12/95   BY: taf *J053*                */
/* REVISION: 8.5      LAST MODIFIED: 01/08/96   BY: jzw *H0K0*                */
/* REVISION: 8.5      LAST MODIFIED: 04/11/96   BY: *J04C* Sue Poland         */
/* REVISION: 8.5      LAST MODIFIED: 04/11/96   BY: *J04C* Markus Barone      */
/* REVISION: 8.5      LAST MODIFIED: 06/10/96   BY: *J0RX* Sue Poland         */
/* REVISION: 8.5      LAST MODIFIED: 06/24/96   BY: *J0WF* Sue Poland         */
/* REVISION: 8.5      LAST MODIFIED: 07/22/96   BY: *J10J* Kieu Nguyen        */
/* REVISION: 8.5      LAST MODIFIED: 08/02/96   BY: *J13H* Jean Miller        */
/* REVISION: 8.6      LAST MODIFIED: 11/25/96   BY: *K01X* jzw                */
/* REVISION: 8.6      LAST MODIFIED: 03/18/97   BY: *J1L9* Aruna Patil        */
/* REVISION: 8.6      LAST MODIFIED: 10/09/97   BY: *K0JV* Surendra Kumar     */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 05/05/98   BY: *L00L* E. vdGevel         */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan         */
/* REVISION: 8.6E     LAST MODIFIED: 06/23/98   BY: *L01G* Robin McCarthy     */
/* REVISION: 8.6E     LAST MODIFIED: 06/24/98   BY: *L02W* Russ Witt          */
/* REVISION: 8.6E     LAST MODIFIED: 01/22/99   BY: *J38T* Poonam Bahl        */
/* REVISION: 8.6E     LAST MODIFIED: 02/10/99   BY: *L0D4* Satish Chavan      */
/* REVISION: 8.6E     LAST MODIFIED: 05/07/99   BY: *J3DQ* Niranjan R.        */
/* REVISION: 8.6E     LAST MODIFIED: 09/28/99   BY: *L0J4* Satish Chavan      */
/* REVISION: 9.1      LAST MODIFIED: 02/24/00   BY: *M0K0* Ranjit Jain        */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane   */
/* REVISION: 9.1      LAST MODIFIED: 07/06/00   BY: *N0F4* Mudit Mehta        */
/* REVISION: 9.1      LAST MODIFIED: 09/05/00   BY: *N0RF* Mark Brown         */
/* REVISION: 9.1      LAST MODIFIED: 09/06/00   BY: *N0D0* Santosh Rao        */
/* REVISION: 9.1      LAST MODIFIED: 10/16/00   BY: *N0WC* Mudit Mehta        */
/* Revision: 1.24       BY: Katie Hilbert        DATE: 04/01/01  ECO: *P002*  */
/* Revision: 1.25       BY: Ellen Borden         DATE: 07/06/01  ECO: *P007*  */
/* Revision: 1.26       BY: Ashwini G.           DATE: 01/10/02  ECO: *L194*  */
/* Revision: 1.27       BY: Jean Miller          DATE: 04/09/02  ECO: *P058*  */
/* $Revision: 1.28 $      BY: Manisha Sawant       DATE: 07/11/02  ECO: *N1NW*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdeclre.i}
{cxcustom.i "SOSOTRL2.I"}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* NEW SHARED VARIABLES, BUFFERS AND FRAMES */
define new shared variable undo_txdetrp     like mfc_logical.
define new shared variable tax_recno        as recid.

/* l_txchg IS SET TO TRUE IN TXEDIT.P WHEN TAXES ARE BEING EDITED  */
/* AND NOT JUST VIEWED IN DR/CR MEMO MAINTENANCE                   */
define new shared variable l_txchg     like mfc_logical initial no.

/* SHARED VARIABLES, BUFFERS AND FRAMES */
define shared variable rndmthd          like rnd_rnd_mthd.
define shared variable display_trail    like mfc_logical.
define shared variable so_recno         as recid.
define shared variable maint            like mfc_logical.
define shared variable taxable_amt      as decimal
   format "->>>>,>>>,>>9.99"
   label "Taxable".
define shared variable line_taxable_amt like taxable_amt.
define shared variable nontaxable_amt   like taxable_amt label "Non-Taxable".
define shared variable line_total       as decimal
   format "-zzzz,zzz,zz9.99"
   label "Line Total".
define shared variable disc_amt         like line_total
   label "Discount"
   format "(zzzz,zzz,zz9.99)".
define shared variable tax_amt          like line_total label "Total Tax".
define shared variable ord_amt          like line_total label "Total".
define shared variable user_desc        like trl_desc extent 3.
define shared variable total_pst        like line_total.
define shared variable tax_date         like so_tax_date.
define shared variable new_order        like mfc_logical.
define shared variable tax_edit         like mfc_logical.
define shared variable tax_edit_lbl     like mfc_char format "x(28)".
define shared variable invcrdt          as character  format "x(15)".
define shared variable undo_trl2        like mfc_logical.
define shared variable due_date_range   like mfc_logical.
define shared variable date_range       like sod_due_date.
define shared variable date_range1      like sod_due_date.
define shared variable l_cr_ord_amt     like line_total  no-undo.
define  VARIABLE msg-arg as character no-undo.
define shared  variable container_charge_total as decimal
   format "->>>>>>>>9.99"
   label "Containers" no-undo.
define  shared variable line_charge_total as decimal
   format "->>>>>>>>9.99"
   label "Line Charges" no-undo.

define shared frame sotot.

/* LOCAL VARIABLES, BUFFERS AND FRAMES */
define variable tax_tr_type     like tx2d_tr_type initial "11".
define variable tax_nbr         like tx2d_nbr initial "".
define variable tax_lines       like tx2d_line initial 0.
define variable disc_pct        like so_disc_pct.
define variable page_break      as integer initial 10.
define variable col-80          as logical initial true.
define variable recalc          like mfc_logical initial true.
define variable credit_hold     like mfc_logical no-undo.
{&SOSOTRL2-I-TAG1}
define variable base_amt        like ar_amt.
define variable retval          as integer.
define variable balance_fmt     as character.
define variable limit_fmt       as character.
define variable ext_line_actual like sod_price no-undo.
define variable l_cr_line_total as   decimal    no-undo.
define variable l_cr_disc_amt   like line_total no-undo.

{fsconst.i} /* FIELD SERVICE CONSTANTS */
{txcalvar.i}

{etdcrvar.i}  /* TOOLKIT DUAL CURRENCY PRICING VARIABLES */
{etvar.i}     /* TOOLKIT GENERAL VARIABLES */
{etrpvar.i}   /* TOOLKIT REPORTING CURRENCY VARIABLES */
{gpfilev.i}   /* VARIABLE DEFINITIONS FOR gpfile.i */

{cclc.i}  /* DETERMINE IF CONTAINER AND LINE CHARGES ARE ENABLED */

find first gl_ctrl no-lock.
find first soc_ctrl no-lock.

/* SET LIMIT_FMT ACCORDING TO BASE CURR ROUND METHOD*/
limit_fmt = "->>>>,>>>,>>9.99".
{gprun.i ""gpcurfmt.p""
   "(input-output limit_fmt,
     input gl_rnd_mthd)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


/* SET BALANCE_FMT ACCORDING TO BASE CURR ROUND METHOD*/
balance_fmt = "->>>>,>>>,>>9.99".
{gprun.i ""gpcurfmt.p""
   "(input-output balance_fmt,
     input gl_rnd_mthd)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


if maint then /* LOCK THE RECORD */
   find so_mstr where recid(so_mstr) = so_recno exclusive-lock.
else /* PRINTING - DON'T LOCK THE RECORD */
   find so_mstr where recid(so_mstr) = so_recno no-lock.

tax_nbr = so_quote. /*TVO*/

if so_fsm_type = 'RMA' then
   assign tax_tr_type = '36'.

/* USE TRANSACTION TYPE 38 FOR CALL INVOICE RECORDING (SSM) */
/* AND SET THE TAX_NBR TO THE CALL'S QUOTE (IF ANY) */
if so_fsm_type = fsmro_c then do:

   for first ca_mstr
   fields(ca_category ca_nbr ca_quote_nbr)
      where ca_category = '0'
      and   ca_nbr      = so_nbr no-lock:
   end.

   if available ca_mstr then
      tax_nbr = ca_quote_nbr.
   tax_tr_type = "38".

end.

/**** FORMS ****/

/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
   

&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
&ENDIF /*GUI*/
so_nbr
   so_cust
   so_bill
   so_ship


&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN
 SKIP(.4)  /*GUI*/
&ENDIF /*GUI*/
with frame a side-labels width 80 attr-space
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN
 NO-BOX THREE-D /*GUI*/
&ENDIF /*GUI*/
.


&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN
 DEFINE VARIABLE F-a-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
 RECT-FRAME-LABEL:HIDDEN in frame a = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame a =
  FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME a = FRAME a:WIDTH-CHARS - .5.  /*GUI*/
&ENDIF /*GUI*/

/*GUI preprocessor Frame A undefine */
&UNDEFINE PP_FRAME_NAME



/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).

{sosomt01.i}  /* Define shared frame d */
{socurvar.i}
{txcurvar.i}
{sototfrm.i}
{ettotfrm.i}

tax_nbr = so_quote.

if not maint and display_trail then do:
   tax_edit_lbl = "".
   undo_txdetrp = true.
   {gprun.i  ""txdetrp.p"" "(input tax_tr_type,
        input so_nbr,
        input tax_nbr,
        input col-80,
        input page_break)" }
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

   if undo_txdetrp = true then undo, leave.
   if page-size - line-counter < page_break then page.
   do while page-size - line-counter > page_break:
      put skip(1).
   end.
   put
      "-----------------------------------------" +
      "-----------------------------------------" format "x(80)".
end.

taxloop:
do on endkey undo, leave:

   /*** GET TOTALS FOR LINES ***/
   assign
      l_cr_line_total         = 0
      line_total              = 0
      taxable_amt             = 0
      nontaxable_amt          = 0
      container_charge_total  = 0
      line_charge_total       = 0.

   if so_tax_date = ? then
      tax_date = so_due_date.
   else
      tax_date = so_tax_date.
   if tax_date = ? then
      tax_date = so_ord_date.

   /* ACCUMULATE LINE AMOUNTS */
   for each sod_det where sod_nbr = so_nbr {&sod_for_each} no-lock:
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


      /* NEW SET OF VARIABLES ARE INTRODUCED TO CALCULATE THE ORDER */
      /* AMOUNT WHICH IS USED IN CREDIT HOLD LOGIC AND THE EXISTING */
      /* SET OF VARIABLES ARE USED TO CALCULATE THE OPEN AMOUNT     */

      if using_line_charges then do:

         for each sodlc_det
         fields(sodlc_order sodlc_ord_line sodlc_ext_price
                sodlc_one_time sodlc_times_charged sodlc_trl_code)
         where sodlc_order = sod_nbr and
               sodlc_ord_line = sod_line
         no-lock:
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


            if sodlc_one_time and sodlc_times_charged > 0 then next.

            ext_line_actual = sodlc_ext_price.

            /* ROUND PER DOCUMENT CURRENCY ROUND METHOD */
            {gprunp.i "mcpl" "p" "mc-curr-rnd"
               "(input-output ext_line_actual,
                 input rndmthd,
                 output mc-error-number)"}
            if mc-error-number <> 0 then do:
               {pxmsg.i &MSGNUM = mc-error-number &ERRORLEVEL = 2}
            end.
            assign
               line_charge_total = line_charge_total + ext_line_actual
               line_total = line_total + ext_line_actual.

            for first trl_mstr fields (trl_code trl_taxable)
               where trl_code = sodlc_trl_code no-lock:
            end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

            if available trl_mstr then do:
               if trl_taxable then
                  taxable_amt = taxable_amt + ext_line_actual.
               else
                  nontaxable_amt = nontaxable_amt + ext_line_actual.
            end. /*IF AVAILABLE TRL_MSTR*/

         end. /* FOR EACH SODLC_DET*/

      end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/
 /*IF USING_LINE_CHARGES*/

      run p-line-total
         (input        rndmthd,
          input        mc-error-number,
          input-output line_total,
          input-output l_cr_line_total,
          input-output taxable_amt,
          input-output line_taxable_amt,
          input-output nontaxable_amt,
          buffer       sod_det).

   end.

   {&SOSOTRL2-I-TAG2}
   if maint and new_order then do: /* Sales volume discount */
      {gprun.i ""sosd.p"" "(input so_ord_date,
           input so_ex_rate,
           input so_ex_rate2,
           input so_cust,
           input so_curr,
           input line_total,
           output disc_pct)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

      if disc_pct > so_disc_pct and disc_pct <> 0 then
         so_disc_pct = disc_pct.
   end.

   run p-disc-taxable-nontaxable-amt
      (input        rndmthd,
       input        mc-error-number,
       input        line_total,
       input        l_cr_line_total,
       input        so_disc_pct,
       input-output disc_amt,
       input-output l_cr_disc_amt,
       input-output taxable_amt,
       input-output nontaxable_amt).

   /* ADD TRAILER AMOUNTS */
   {txtrltrl.i so_trl1_cd so_trl1_amt user_desc[1]}
   {txtrltrl.i so_trl2_cd so_trl2_amt user_desc[2]}
   {txtrltrl.i so_trl3_cd so_trl3_amt user_desc[3]}

   /* CHECK PREVIOUS DETAIL FOR EDITED VALUES */
   find first tx2d_det where tx2d_ref = so_nbr      and
      tx2d_nbr = so_quote    and
      tx2d_tr_type = tax_tr_type and
      tx2d_edited
   no-lock no-error.
   if available(tx2d_det) then do:
      /* Previous tax values edited. Recalculate? */
      {pxmsg.i &MSGNUM=917 &ERRORLEVEL=2 &CONFIRM=recalc}
   end.

   if maint and recalc then do:
      /* CALCULATE TAXES */
      /* NOTE nbr FIELD BLANK FOR SALES ORDERS */
      /* ADDED TWO PARAMETERS TO TXCALC.P, INPUT PARAMETER VQ-POST
       *  AND OUTPUT PARAMETER RESULT-STATUS. THE POST FLAG IS SET
       *  TO 'NO' BECAUSE WE ARE NOT CREATING QUANTUM REGISTER
       *  RECORDS FROM THIS CALL TO TXCALC.P */
      {gprun.i ""txcalc.p""
         "(input  tax_tr_type,
           input  so_nbr,
           input  tax_nbr,
           input  tax_lines, /*ALL LINES*/
           input  no,
           output result-status)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

   end. /* if maint */

   /* PROCEDURE CALCULATES TOTAL TAXABLE AND  NONTAXABLE AMOUNT */
   run p-tottax
      (input  tax_tr_type,
       input  so_nbr,
       input  tax_nbr,
       input  tax_lines,
       input-output taxable_amt,
       input-output nontaxable_amt).

   {gprun.i ""txabsrb.p"" "(input so_nbr,
        input so_quote,
        input tax_tr_type,
        input-output line_total,
        input-output taxable_amt)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


   run p-linetax-incltax-ord-amt
      (input        tax_tr_type,
       input        tax_nbr,
       input        tax_lines,
       input        total_pst,
       input        l_cr_disc_amt,
       input        taxable_amt,
       input        disc_amt,
       input-output tax_amt,
       input-output line_total,
       input-output l_cr_line_total,
       input-output line_taxable_amt,
       input-output ord_amt,
       input-output l_cr_ord_amt,
       buffer       so_mstr).

   /* CHECK CREDIT AMOUNTS */
   if ord_amt < 0 then
      invcrdt = "**" + getTermLabel("C_R_E_D_I_T",11) + "**".
   else
      invcrdt = "".

   /* IF IN SO MAINT. THEN RECALCULATE TAXES AFTER EDITING */
   if maint then do:

      /* CHECK CREDIT LIMIT */
      /* If the bill-to customer's outstanding balance is already above   */
      /* F4 out of the trailer screen, bypassing the check done after     */
      /* Note that we don't bother checking if we're not going to put the */
      if so_stat = "" and soc_cr_hold
      then do:

         find cm_mstr where cm_addr = so_bill no-lock.

         base_amt = ord_amt.

         if so_curr <> base_curr then do:
            /* CONVERT FROM FOREIGN TO BASE CURRENCY */
            {gprunp.i "mcpl" "p" "mc-curr-conv"
               "(input so_curr,
                 input base_curr,
                 input so_ex_rate,
                 input so_ex_rate2,
                 input base_amt,
                 input true, /*  ROUND */
                 output base_amt,
                 output mc-error-number)"}.
         end.

         /* NOTE: DO NOT PUT CALL REPAIR ORDERS (FSM-RO) ON HOLD - BECAUSE */
         /*    THESE ORDERS WILL NOT BE SHIPPING ANYTHING, ONLY INVOICING  */
         /*    FOR WORK ALREADY DONE.                                      */
         if cm_cr_limit < (cm_balance + base_amt)
            and so_fsm_type <> "FSM-RO"
         then do:
            /* Sales Order placed on credit hold */
            credit_hold = true.
            so_stat = "HD".
            display so_stat with frame d
&IF ("{&PP_GUI_CONVERT_MODE}" = "REPORT") &THEN
 STREAM-IO /*GUI*/ 
&ENDIF /*GUI*/
.
            msg-arg = string((cm_balance + base_amt),balance_fmt).
            /* Customer Balance plus this Order */
            {pxmsg.i &MSGNUM=616 &ERRORLEVEL=2 &MSGARG1=msg-arg}
            msg-arg = string(cm_cr_limit,limit_fmt).
            /* Credit Limit */
            {pxmsg.i &MSGNUM=617 &ERRORLEVEL=1 &MSGARG1=msg-arg}
            /* # Placed on Credit Hold */
            {pxmsg.i &MSGNUM=690 &ERRORLEVEL=1
                     &MSGARG1=getTermLabel(""SALES_ORDER"",20)}
         end.

      end.

      {etdcrc.i so_curr so_mstr.so}
      {sototdsp.i} /* DISPLAY ALL FIELDS */

      trlloop:
      do on error undo, retry
         on endkey undo taxloop, leave:
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


         {&SOSOTRL2-I-TAG3}
         set
            so_disc_pct so_trl1_cd so_trl1_amt so_trl2_cd
            so_trl2_amt so_trl3_cd so_trl3_amt tax_edit
         with frame sotot.
         {&SOSOTRL2-I-TAG4}

         {txedttrl.i &code  = "so_trl1_cd"
            &amt   = "so_trl1_amt"
            &desc  = "user_desc[1]"
            &frame = "sotot"
            &loop  = "trlloop"}

         /* VALIDATE TRAILER AMOUNT BASE ON ROUNDING METHOD */
         if (so_trl1_amt <> 0) then do:
            {gprun.i ""gpcurval.p""
               "(input so_trl1_amt,
                 input rndmthd,
                 output retval)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

            if (retval <> 0) then do:
               next-prompt so_trl1_amt with frame sotot.
               undo trlloop, retry.
            end.
         end.

         {txedttrl.i &code  = "so_trl2_cd"
            &amt   = "so_trl2_amt"
            &desc  = "user_desc[2]"
            &frame = "sotot"
            &loop  = "trlloop"}

         /* VALIDATE TRAILER AMOUNT BASE ON ROUNDING METHOD */
         if (so_trl2_amt <> 0) then do:
            {gprun.i ""gpcurval.p""
               "(input so_trl2_amt,
                 input rndmthd,
                 output retval)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

            if (retval <> 0) then do:
               next-prompt so_trl2_amt with frame sotot.
               undo trlloop, retry.
            end.
         end.

         {txedttrl.i &code  = "so_trl3_cd"
            &amt   = "so_trl3_amt"
            &desc  = "user_desc[3]"
            &frame = "sotot"
            &loop  = "trlloop"}

         /* VALIDATE TRAILER AMOUNT BASE ON ROUNDING METHOD */
         if (so_trl3_amt <> 0) then do:
            {gprun.i ""gpcurval.p""
               "(input so_trl3_amt,
                 input rndmthd,
                 output retval)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

             if (retval <> 0) then do:
                next-prompt so_trl3_amt with frame sotot.
                undo trlloop, retry.
             end.
         end.

      end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/
 /* TRLLOOP: DO ON ERROR UNDO, RETRY */

      /*** RECALCULATE TOTALS FOR LINES ***/
      assign
         l_cr_line_total        = 0
         line_total             = 0
         taxable_amt            = 0
         nontaxable_amt         = 0
         container_charge_total = 0
         line_charge_total      = 0.

      /* ACCUMULATE LINE AMOUNTS */
      for each sod_det where sod_nbr = so_nbr {&sod_for_each}
      no-lock:
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


         if using_line_charges then do:
            for each sodlc_det
            fields(sodlc_order sodlc_ord_line sodlc_ext_price
                   sodlc_one_time sodlc_times_charged sodlc_trl_code)
            where sodlc_order = sod_nbr and
                  sodlc_ord_line = sod_line
            no-lock:
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


               if sodlc_one_time and sodlc_times_charged > 0 then next.

               ext_line_actual = sodlc_ext_price.

               /* ROUND PER DOCUMENT CURRENCY ROUND METHOD */
               {gprunp.i "mcpl" "p" "mc-curr-rnd"
                  "(input-output ext_line_actual,
                    input rndmthd,
                    output mc-error-number)"}
               if mc-error-number <> 0 then do:
                  {pxmsg.i &MSGNUM = mc-error-number &ERRORLEVEL = 2}
               end.

               assign
                  line_charge_total = line_charge_total + ext_line_actual
                  line_total = line_total + ext_line_actual.

               for first trl_mstr fields( trl_code trl_taxable)
                  where trl_code = sodlc_trl_code no-lock:
               end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


               if available trl_mstr then do:
                  if trl_taxable then
                     taxable_amt = taxable_amt + ext_line_actual.
                  else
                     nontaxable_amt = nontaxable_amt + ext_line_actual.
               end. /*IF AVAILABLE TRL_MSTR*/

            end. /* FOR EACH SODLC_DET*/

         end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/
 /*IF USING_LINE_CHARGES*/

         run p-line-total
            (input        rndmthd,
            input        mc-error-number,
            input-output line_total,
            input-output l_cr_line_total,
            input-output taxable_amt,
            input-output line_taxable_amt,
            input-output nontaxable_amt,
            buffer       sod_det).

      end.

      run p-disc-taxable-nontaxable-amt
         (input        rndmthd,
          input        mc-error-number,
          input        line_total,
          input        l_cr_line_total,
          input        so_disc_pct,
          input-output disc_amt,
          input-output l_cr_disc_amt,
          input-output taxable_amt,
          input-output nontaxable_amt).

      /* ADD TRAILER AMOUNTS */
      {txtrltrl.i so_trl1_cd so_trl1_amt user_desc[1]}
      {txtrltrl.i so_trl2_cd so_trl2_amt user_desc[2]}
      {txtrltrl.i so_trl3_cd so_trl3_amt user_desc[3]}

      /****** CALCULATE TAXES ************/
      if recalc then do:
         /* ADDED TWO PARAMETERS TO TXCALC.P, INPUT PARAMETER VQ-POST
         *  AND OUTPUT PARAMETER RESULT-STATUS. THE POST FLAG IS SET
         *  TO 'NO' BECAUSE WE ARE NOT CREATING QUANTUM REGISTER
         *  RECORDS FROM THIS CALL TO TXCALC.P */
         {gprun.i ""txcalc.p""  "(input  tax_tr_type,
              input  so_nbr,
              input  tax_nbr,
              input  tax_lines, /*ALL*/
              input  no,
              output result-status)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

      end.

      /* PROCEDURE CALCULATES TOTAL TAXABLE AND  NONTAXABLE AMOUNT */
      run p-tottax
         (input  tax_tr_type,
          input  so_nbr,
          input  tax_nbr,
          input  tax_lines,
          input-output taxable_amt,
          input-output nontaxable_amt).

      {gprun.i ""txabsrb.p"" "(input so_nbr,
           input so_quote,
           input tax_tr_type,
           input-output line_total,
           input-output taxable_amt)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


      /* DO TAX DETAIL DISPLAY / EDIT HERE */
      if tax_edit then do:
         hide frame sotot no-pause.
         hide frame d no-pause.
         {gprun.i ""txedit.p""  "(input  tax_tr_type,
              input  so_nbr,
              input  tax_nbr,
              input  tax_lines, /*ALL LINES*/
              input  so_tax_env,
              output tax_amt)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

         view frame sotot.
         /*V8+*/
      end.

      run p-linetax-incltax-ord-amt
         (input        tax_tr_type,
          input        tax_nbr,
          input        tax_lines,
          input        total_pst,
          input        l_cr_disc_amt,
          input        taxable_amt,
          input        disc_amt,
          input-output tax_amt,
          input-output line_total,
          input-output l_cr_line_total,
          input-output line_taxable_amt,
          input-output ord_amt,
          input-output l_cr_ord_amt,
          buffer       so_mstr).

      /* CHECK CREDIT AMOUNTS */
      if ord_amt < 0 then
         invcrdt = "**" + getTermLabel("C_R_E_D_I_T",11) + "**".
      else
         invcrdt = "".

   end. /* IF MAINT */

   if display_trail then do:
      {etdcrc.i so_curr so_mstr.so}
      {sototdsp.i} /* DISPLAY ALL FIELDS */
   end.

   undo_trl2 = false.

end. /* taxloop*/

/* Warn user now if order had been put on credit hold (now */
if maint and credit_hold then do:
   so_stat = "HD".
end.

/* PROCEDURE CALCULATES TOTAL TAXABLE AND  NONTAXABLE AMOUNT */
{txtotal.i}

/* CREATED FOLLOWING THREE INTERNAL PROCEDURES p-line-total,   */
/* p-disc-taxable-nontaxable-amt AND p-linetax-incltax-ord-amt */
/* TO AVOID CODE REDUNDANCY                                    */

PROCEDURE p-line-total:

   define input        parameter l_rndmthd      like rnd_rnd_mthd
      no-undo.
   define input        parameter l_mc_error_nbr like msg_nbr no-undo.
   define input-output parameter l_line_tot     as   decimal no-undo.
   define input-output parameter l_cr_line_tot  as   decimal no-undo.
   define input-output parameter l_tx_amt       as   decimal no-undo.
   define input-output parameter l_line_tx_amt  as   decimal no-undo.
   define input-output parameter l_nontx_amt    as   decimal no-undo.
   define              parameter buffer sod_det for  sod_det.

   define              variable l_ext_actual    like sod_price no-undo.
   define              variable l_cr_ext_actual like sod_price no-undo.
   assign
      l_ext_actual    = ( sod_price   * (sod_qty_ord - sod_qty_ship))
      l_cr_ext_actual = ( sod_price   *
      ((sod_qty_ord - sod_qty_ship) + sod_qty_inv)).

   /* ROUND PER DOCUMENT CURRENCY ROUND METHOD */
   {gprunp.i "mcpl" "p" "mc-curr-rnd"
      "(input-output l_ext_actual,
        input        l_rndmthd,
        output       l_mc_error_nbr)"}

   {gprunp.i "mcpl" "p" "mc-curr-rnd"
      "(input-output l_cr_ext_actual,
        input        l_rndmthd,
        output       l_mc_error_nbr)"}

   assign
      l_line_tot    = (l_line_tot + l_ext_actual)
      l_cr_line_tot = (l_cr_line_tot + l_cr_ext_actual).

   if sod_taxable then
      assign
         l_tx_amt      = (l_tx_amt + l_ext_actual)
         l_line_tx_amt = l_tx_amt.
   else
      l_nontx_amt = (l_nontx_amt + l_ext_actual).

END PROCEDURE. /* p-line-total */

PROCEDURE p-disc-taxable-nontaxable-amt:

   define input        parameter l_rndmthd      like rnd_rnd_mthd
      no-undo.
   define input        parameter l_mc_error_nbr like msg_nbr no-undo.
   define input        parameter l_line_tot     as   decimal no-undo.
   define input        parameter l_cr_line_tot  as   decimal no-undo.
   define input        parameter l_so_disc_pct  like so_disc_pct
      no-undo.
   define input-output parameter l_dis_amt      as   decimal no-undo.
   define input-output parameter l_cr_dis_amt   as   decimal no-undo.
   define input-output parameter l_tx_amt       as   decimal no-undo.
   define input-output parameter l_nontx_amt    as   decimal no-undo.

   define              variable  l_tmp_amt      as   decimal no-undo.

   assign
      l_dis_amt    = (- l_line_tot    * (l_so_disc_pct / 100))
      l_cr_dis_amt = (- l_cr_line_tot * (l_so_disc_pct / 100))
      l_tmp_amt    = (  l_tx_amt      * (l_so_disc_pct / 100)).

   /* ROUND PER DOCUMENT CURRENCY ROUND METHOD */
   {gprunp.i "mcpl" "p" "mc-curr-rnd"
      "(input-output l_dis_amt,
        input        l_rndmthd,
        output       l_mc_error_nbr)"}

   {gprunp.i "mcpl" "p" "mc-curr-rnd"
      "(input-output l_cr_dis_amt,
        input        l_rndmthd,
        output       l_mc_error_nbr)"}

   {gprunp.i "mcpl" "p" "mc-curr-rnd"
      "(input-output l_tmp_amt,
        input        l_rndmthd,
        output       l_mc_error_nbr)"}

   assign
      l_tx_amt   = (l_tx_amt - l_tmp_amt)
      l_tmp_amt  = (l_nontx_amt * (l_so_disc_pct / 100)).

   /* ROUND PER DOCUMENT CURRENCY ROUND METHOD */
   {gprunp.i "mcpl" "p" "mc-curr-rnd"
      "(input-output l_tmp_amt,
        input        l_rndmthd,
        output       l_mc_error_nbr)"}

   l_nontx_amt = (l_nontx_amt - l_tmp_amt).

END PROCEDURE. /* p-disc-taxable-nontaxable-amt */

PROCEDURE p-linetax-incltax-ord-amt:

   define input        parameter l_tx_tr_type   like tx2d_tr_type
      no-undo.
   define input        parameter l_tx_nbr       like tx2d_nbr  no-undo.
   define input        parameter l_tx_lines     like tx2d_line no-undo.
   define input        parameter l_total_pst    as   decimal   no-undo.
   define input        parameter l_cr_dis_amt   as   decimal   no-undo.
   define input        parameter l_txa_amt      as   decimal   no-undo.
   define input        parameter l_dis_amt      as   decimal   no-undo.
   define input-output parameter l_tx_amt       as   decimal   no-undo.
   define input-output parameter l_line_tot     as   decimal   no-undo.
   define input-output parameter l_cr_line_tot  as   decimal   no-undo.
   define input-output parameter l_line_tx_amt  as   decimal   no-undo.
   define input-output parameter l_ord_amt      as   decimal   no-undo.
   define input-output parameter l_crd_ord_amt  as   decimal   no-undo.
   define              parameter buffer so_mstr for  so_mstr.

   define              variable  l_tax_in       as   decimal   no-undo.
   define              variable  l_cr_tx_amt    as   decimal   no-undo.
   define              variable  l_cr_l_tx_in   as   decimal   no-undo.

   /* GET TAX TOTALS */
   {gprun.i ""txtotal.p""
      "(input  l_tx_tr_type,
        input  so_nbr,
        input  l_tx_nbr,
        input  l_tx_lines, /* ALL LINES */
        output l_tx_amt)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


   /* OBTAINING TOTAL INCLUDED TAX FOR THE TRANSACTION */
   {gprun.i ""txtotal1.p""
      "(input  l_tx_tr_type,
        input  so_nbr,
        input  l_tx_nbr,
        input  l_tx_lines, /* ALL LINES */
        output l_tax_in)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


   /* OBTAINING TOTAL TAX AND INCLUDED TAX FOR THE SHIPPED ORDERED */
   /* LINES WHERE INVOICE IS NOT PRINTED OR POSTED                 */
   if l_cr_line_tot <> l_line_tot
   then do:

      {gprun.i ""txtotal.p""
         "(input  ""13"",
           input  so_nbr,
           input  l_tx_nbr,
           input  l_tx_lines, /* ALL LINES */
           output l_cr_tx_amt)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


      {gprun.i ""txtotal1.p""
         "(input  ""13"",
           input  so_nbr,
           input  l_tx_nbr,
           input  l_tx_lines, /* ALL LINES */
           output l_cr_l_tx_in)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


   end. /* IF l_cr_line_tot <> l_line_tot */

   /* ADJUSTING LINE TOTALS AND TOTAL TAX BY INCLUDED TAX */
   assign
      l_line_tot    = (l_line_tot - l_tax_in)
      l_cr_line_tot = (l_cr_line_tot - (l_tax_in + l_cr_l_tx_in))
      l_line_tx_amt =  l_txa_amt
      l_tx_amt      = (l_tx_amt + l_tax_in)
      l_cr_tx_amt   = ((l_tx_amt + l_cr_tx_amt) +
                       (l_tax_in + l_cr_l_tx_in))

      /* CALCULATE ORDER TOTAL */
      l_ord_amt     = (l_line_tot  + l_dis_amt   +
                       so_trl1_amt + so_trl2_amt +
                       so_trl3_amt + l_tx_amt    +
                       l_total_pst)

      /* CALCULATE ORDER TOTAL FOR CREDIT LIMIT LOGIC */
      l_crd_ord_amt = (l_cr_line_tot + l_cr_dis_amt +
                       so_trl1_amt   + so_trl2_amt  +
                       so_trl3_amt   + l_cr_tx_amt  +
                       l_total_pst).

END PROCEDURE. /* p-linetax-incltax-ord-amt */
